/* *****************************************************************************
 *
 *	danizone.ch
 *
 *	Developer:		mda010
 *	Creation date:	May 6, 2012 10:09:17 PM
 *	
 *	Project name:	HomegrownCuisine
 *	File name:		AdminController.java
 *
 *	Copyrights (C) 2012 @danizone.ch
 *
 *   This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *	GNU General Public License for more details.
 *
 *	Change history:	[developer]; [date];[reason for change]
 *
 *					mda010; May 6, 2012; created
 *
 ***************************************************************************** */
package ch.zhaw.hgc.controllers;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import ch.zhaw.hgc.entities.Category;
import ch.zhaw.hgc.entities.Recipe;
import ch.zhaw.hgc.services.ICategroyService;

/**
 * @author Remo Zollinger 
 * Description: Provides the basic functionality to add a category.
 */
@Controller
@RequestMapping("/admin/addCategory")
public class AddCategoryController {
	
	private static final Logger logger = Logger
			.getLogger(AddCategoryController.class);
	
	List<Category> categories = null;

	@Autowired
	private ICategroyService categoryService;

	/**
	 * Get Method for category.
	 * @param recipe a Recipe
	 * @return the Model view
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView get(Recipe recipe) {
		ModelAndView model = new ModelAndView("admin/addCategory");		
		model.addObject("category", new Category());				
		model.addObject("categories", categoryService.findAll());
		model.addObject("message", "");
		logger.info("Passed get() method in controller...");

		return model;
	}

	/**
	 * Post Method adding a category.
	 * @param category the category
	 * @return the Model view
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView submitSeason(@ModelAttribute("category") Category category){
		ModelAndView model = new ModelAndView("admin/addCategory");
		categoryService.save(category);				
		model.addObject("category", new Category());
		model.addObject("categories", categoryService.findAll());		
		model.addObject("message", "Successfull saved");
		return model;
	}
}